// 购物车状态
export default {
  namespaced: true,
  state: () => {
    return {
      list: []
    }
  },
  mutations: {
    // 添加商品
    insertCart (state, goods) {
      const good = state.list.find(item => item.skuId === goods.skuId)
      if (good) {
        good.count += goods.count
      } else {
        state.list.push(goods)
      }
    },
    // 删除商品
    deleteCart (state, skuId) {
      const idx = state.list.findIndex(item => item.skuId === skuId)
      if (idx > -1) {
        state.list.splice(idx, 1)
      }
    },
    // 修改购物车中的商品
    updateCart (state, goods) {
      // 1. 找到这个商品
      const obj = state.list.find(it => it.skuId === goods.skuId)
      // 2. 更新商品状态
      if (obj) {
        for (const p in obj) {
          if (goods[p] !== undefined && goods[p] !== null) {
            obj[p] = goods[p]
          }
        }
      }
    },
    // 修改sku
    updateCartSku (state, { newSku, oldSkuId }) {
      const oldSkuIdx = state.list.findIndex(it => it.skuId === oldSkuId)
      if (oldSkuIdx === -1) {
        return
      }
      const oldSkuCopy = { ...state.list[oldSkuIdx] }
      console.log('老的sku', oldSkuCopy)
      const { oldPrice: price, price: nowPrice, skuId, specsText: attrsText, inventory: stock } = newSku
      // 用新的sku信息 结合老的SKU等到一份 全新的数据
      const newSkuNew = { ...oldSkuCopy, ...{ price, nowPrice, skuId, attrsText, stock } }

      // 删除老的sku添加新数据
      state.list.splice(oldSkuIdx, 1, newSkuNew)

      console.log('当前购物车', state.list)
      console.log('新的sku', newSkuNew)
    }
  },
  actions: {
    insertCart (ctx, goods) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // todo
          console.log('按登录的去做')
        } else { // 没有登录
          console.log('actionsinsertCart', goods)
          // 调用mutations
          ctx.commit('insertCart', goods)
          resolve()
        }
      })
    },
    deleteCart (ctx, skuId) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // todo
          console.log('按登录的去做')
        } else {
          // 调用mutations
          ctx.commit('deleteCart', skuId)
          resolve()
        }
      })
    },
    updateCart (ctx, goods) {
      // goods 中：必须有skuId，其他想修改的属性 selected  count
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // 登录 TODO
        } else {
          // 本地
          ctx.commit('updateCart', goods)
          resolve()
        }
      })
    },
    // 把所有有效商品统一设置selected值
    checkAllCart (ctx, selected) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // todo
          console.log('按登录的去做')
        } else {
          // 没有登录
          // 对有效商品列表数据，循序调用mutations，更新选中的状态
          ctx.getters.effectiveList.forEach(it => {
            ctx.commit('updateCart', { skuId: it.skuId, selected: selected })
          })
          resolve()
        }
      })
    },
    // 批量删除选中商品
    batchDeleteCart (ctx) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // todo
          console.log('按登录的去做')
        } else {
          // 没有登录
          // 对有效商品列表数据，循序调用mutations，更新选中的状态
          ctx.getters.effectiveList.forEach(it => {
            ctx.commit('deleteCart', it.skuId)
          })
          resolve()
        }
      })
    },
    // 批量删除无效商品
    batchDeleteinvalidCart (ctx) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // todo
          console.log('按登录的去做')
        } else {
          // 没有登录
          // 无效商品列表数据，循序调用mutations，更新选中的状态
          ctx.getters.invalidList.forEach(it => {
            ctx.commit('deleteCart', it.skuId)
          })
          resolve()
        }
      })
    },
    // 修改sku规格函数
    updateCartSku (ctx, { oldSkuId, newSku }) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // todo
          console.log('按登录的去做')
        } else { // 没有登录
          ctx.commit('updateCartSku', { oldSkuId, newSku })
          resolve()
        }
      })
    }
  },
  getters: {
    // 1. 有效商品列表
    effectiveList (state, getters) {
      return state.list.filter(it => it.isEffective && it.count <= it.stock)
    },

    // 2. 总价格
    allAmount (state, getters) {
      // console.log('所有的getters', getters)
      return getters.effectiveList.reduce((acc, cur) => acc + cur.count * cur.nowPrice, 0)
    },

    // 3. 总件数
    allCount (state, getters) {
      // console.log('所有的getters', getters)
      return getters.effectiveList.reduce((acc, cur) => acc + cur.count, 0)
    },

    // 4. 无效商品
    invalidList (state) {
      return state.list.filter(it => it.stock === 0 || it.isEffective === false)
    },
    // 5. 已选商品列表
    selectedList (state, getters) {
      return getters.effectiveList.filter(it => it.selected)
    },
    // 6. 已选商品总件数
    selectedTotal (state, getters) {
      return getters.selectedList.reduce((acc, cur) => acc + cur.count, 0)
    },
    // 7. 已选商品总金额
    selectedAmount (state, getters) {
      return getters.selectedList.reduce((acc, cur) => acc + cur.count * cur.nowPrice, 0)
    },
    // 8. 是否全选
    isCheckAll (state, getters) {
      return (getters.selectedList.length === getters.effectiveList.length) && (getters.effectiveList.length > 0)
    }
  }
}
